# coding:utf-8
import json
import random
import sys
from sqlalchemy import Column, String, Integer
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from werkzeug.security import check_password_hash, generate_password_hash

Base = declarative_base()


class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    username = Column(String(64), nullable=False, unique=True)
    password = Column(String(64), nullable=False)

    def set_password(self, passwd):
        self.password = generate_password_hash(passwd)

    def check_password(self, passwd):
        return check_password_hash(self.password, passwd)


class UserManager(object):
    """docstring for UserManager"""

    username = None
    password = None
    db_url = "mysql+mysqlconnector://root:newlandedu@139.217.176.104:31306/user"

    def __init__(self, username, password):
        self.username = username
        self.password = password

    def add_user(self):
        new_user = User(username=self.username)
        new_user.set_password(self.password)

        engine = create_engine(self.db_url)
        smaker = sessionmaker(bind=engine)
        session = smaker()
        session.add(new_user)
        session.commit()
        session.close()
        # print("add user " + self.username + " success !")
    def update_user(self):

        engine = create_engine(self.db_url)
        smaker = sessionmaker(bind=engine)
        session = smaker()
        user=session.query(User).filter(User.username == self.username).one()
        user.set_password(self.password)
        session.commit()
        session.close()

def randomNum(N):
    random_number = random.sample('0123456789', N)
    return ''.join(random_number)


def batchAddUser():
     for i in range(17, 19):
         username = 'trial-%02d'%i
         password = randomNum(8)
         print("%s \t%s" % (username, password))
         um = UserManager(username, password)
         um.add_user()

def resetPassword():
    for i in range(17, 19):
        username = 'trial-%02d'%i
        password = randomNum(8)
        print("%s \t%s" % (username, password))
        um = UserManager(username, password)
        um.update_user()

if __name__ == "__main__":
    resetPassword()
    # for i in range(1, 31):
    #     username = 'SD00{}'.format(i)
    #     password = randomNum(8)
    #     print("%s \t%s" % (username, password))
    #     um = UserManager(username, password)
    #     um.add_user()
    #batchAddUser()
    #username = 'test-001'
    #password = 'newlandedu@123'
    #password = '123456'
    #print("%s \t%s" % (username, password))
    #um = UserManager(username, password)
    #um.update_user()
    #um.add_user()

    #um.update_user()
    # import requests
    #
    # url = 'http://competition2.ai.nlecloud.com:30002/hub/api/users'
    # data = {}
    # headers = {"Authorization": "bearer d051893522be49f0a4c2519bcc6397da"}
    # # headers = {'Content-Type':'application/json;charset=UTF-8',"Authorization":"bearer a809655f-e4bf-41b8-9088-85a1d3780d6d"}
    # res = requests.get(url=url, params=data, headers=headers)
    # a=json.loads(res.text)
    # print(res.text)
